package com.zx.dsptuneup_TriMic.widget.ui;

import android.graphics.Typeface;
import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.TextView;
import com.google.android.material.tabs.TabLayout;
import com.zx.dsptuneup_TriMic.R;
import java.util.ArrayList;
import java.util.List;

public class MyWhiteTabLayout extends TabLayout {
    private int leftBtnBgResId = R.drawable.bg_custom_btn_view_left_selector;
    private int rightBtnBgResId = R.drawable.bg_custom_btn_view_right_selector;
    private int middleBtnBgResId = R.drawable.bg_custom_btn_view_middle_selector;
    private float textSize = 16;
    private int textColor = R.color.title_normal;
    private int selectedTextColor = R.color.white;
    private List<String> titles;

    public MyWhiteTabLayout(Context context) {
        super(context);
        init();
    }

    public MyWhiteTabLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyWhiteTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        titles = new ArrayList<>();
        this.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {

            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                /**
                 * 设置当前选中的Tab为特殊高亮样式。
                 */
                if (tab != null && tab.getCustomView() != null) {
                    TextView tab_text = tab.getCustomView().findViewById(R.id.tab_text);

                    tab_text.setScaleY(1.1f);
                    tab_text.setScaleX(1.1f);

                    tab_text.setTextColor(getContext().getResources().getColor(selectedTextColor));
                    tab_text.setGravity(Gravity.CENTER);
                    tab_text.setTextSize(textSize);
                    tab_text.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
                }
            }

            @Override
            public void onTabUnselected(Tab tab) {
                /**
                 * 重置所有未选中的Tab颜色、字体、背景恢复常态(未选中状态)。
                 */
                if (tab != null && tab.getCustomView() != null) {
                    TextView tab_text = tab.getCustomView().findViewById(R.id.tab_text);
                    tab_text.setTextSize(textSize);
                    tab_text.setScaleY(1.0f);
                    tab_text.setScaleX(1.0f);
                    tab_text.setTextColor(getContext().getResources().getColor(textColor));
                    tab_text.setTypeface(Typeface.DEFAULT, Typeface.NORMAL);
                }
            }

            @Override
            public void onTabReselected(Tab tab) {

            }
        });
    }

    public void setTitle(List<String> titles) {
        this.titles = titles;


        /**
         * 开始添加切换的Tab。
         */
        for (String title : this.titles) {
            Tab tab = newTab();
            tab.setCustomView(R.layout.tablayout_white_item);

            int postion = this.titles.indexOf(title);
            if (tab.getCustomView() != null) {
                TextView tab_text = tab.getCustomView().findViewById(R.id.tab_text);
                tab_text.setTextColor(getContext().getResources().getColor(textColor));
                tab_text.setText(title);

                if(titles.size()>1){
                    if(postion==0){
                        tab.getCustomView().setBackgroundResource(leftBtnBgResId);
                    }else if(postion==titles.size()-1){
                        tab.getCustomView().setBackgroundResource(rightBtnBgResId);
                    }else {
                        tab.getCustomView().setBackgroundResource(middleBtnBgResId);
                    }
                }else {
                    // 只有一个居中显示
                    tab.getCustomView().setBackgroundResource(middleBtnBgResId);
//                    this.setTabGravity(GRAVITY_FILL);
//                    this.setTabMode(MODE_FIXED);

                }
            }
            this.addTab(tab);
        }
    }

    /**
     *   数组长度发生变化，采用这种
     * */
    public void refreshTitles(List<String> titles) {
        this.removeAllTabs();
        setTitle(titles);
    }
}
